<script>
const getData = (url, param) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: url,
            method: 'POST',
            data: param,
            header: {
                'Content-Type': 'application/json'
            },

            success(res) {
                resolve(res.data);
            },

            fail(err) {
                reject(err);
            }
        });
    });
};


export default {
    data() {
        return {};
    },
    onLaunch() {

        let SafeCode;
        let WebConfig = {};
        let that = this;
		


        getData(this.globalData.ApiUrl + 'api/Android/webConfig', {}).then((msg) => {
            WebConfig = msg;
            //判断更新
            if (WebConfig.androidVersion > plus.runtime.versionCode) {
                uni.showModal({
                    title: "发现新版本！",
                    content: "更新内容为:\n" + WebConfig.updateContent,
                    success: (res) => {
                        if (res.confirm) {
	
                            uni.showLoading({
                                title: "下载中..."
                            });

                            let url = WebConfig.androidUpdateUrl;
							
							
							url = "https://wyz-1304875448.cos.ap-nanjing.myqcloud.com/apk/app-release.apk";
							
                            // //如果是apk文件，则打开浏览器下载
                            // if (url.indexOf(".apk",0) > 0) {
                            //     plus.runtime.openURL(url);
                            //     return;
                            // }

                            //否则热更新
                            uni.downloadFile({
                                url: url,
                                success: (res) => {
                                    uni.hideLoading();
                                    if (res.statusCode == 200) {
                                        plus.runtime.install(res.tempFilePath, {
                                            force: true
                                        }, (res) => {
                                            plus.runtime.restart();
                                        })
                                    }
                                }
                            })
                        }
                    }
                })
            } else {
                //不更新，直接进入
                uni.getStorage({
                    key: "SafeCode",
                    success: function (res) {
                        SafeCode = res.data;

                        getData(that.globalData.ApiUrl + "api/Android/GetUserData", {
                            SafeCode: SafeCode
                        }).then(
                            (msg) => {
                                //判断是否登录
                                if (msg.result && msg.result === 'error') {
                                    uni.navigateTo({
                                        url: '../../pages/bind/bind'
                                    });
                                }
                            }
                        );

                    },
                    fail: function (res) {
                        uni.navigateTo({
                            url: '../../pages/bind/bind'
                        });
                    }
                });

            }

        });

        //点击消息事件
        plus.push.addEventListener("click", function (msg) {
            setTimeout(() => {
                uni.navigateTo({
                    url: "pages/home/home"
                })
            })
        })
        //接收消息事件
        plus.push.addEventListener("receive", function (msg) {

            console.log("receive:" + msg);

            if (msg.type === 'receive') {
                let options = {
                    cover: false,
                    title: msg.payload.title
                };
                let url = "pages/home/home";
                plus.push.createMessage(msg.payload.content, url, options);
            }

        })


    },

    onReady() {

    },

    globalData: {
        userInfo: {},
        ApiUrl: 'https://school.e-memories.cn/',
        webConfig: {}
    },
    methods: {}
};
</script>
<style>
@import './app.css';
</style>
